package threadjuc;


import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author zxl
 * @version 1.0
 * @date 2021/10/28 8:42
 */
public class Juc11 {
    public static void main(String[] args) {
        //线程数量   限流
        //控制线程进入的数量
        Semaphore semaphore = new Semaphore(3);
        for (int i = 0; i < 20; i++) {
            new Thread(()->{
                try {
                    //得到 ，如果满了，则等待被释放
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"进来了");
                    TimeUnit.SECONDS.sleep(2);
                    System.out.println(Thread.currentThread().getName()+"离开了");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    //释放  当前的线程释放，唤醒等待的线程
                    semaphore.release();
                }
            },String.valueOf(i)).start();
        }
    }
}
